/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Control.Control;
import Logic.Filtro;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Fernando
 */
public class GUI_SelectRule extends javax.swing.JFrame {

    Control control = Control.getInstance();
    private Filtro filtro;
    private GUI_Logminer logminer;
    private String rule;

    public GUI_SelectRule(Filtro fi, GUI_Logminer lgm) {
        initComponents();
        this.tRl.getTableHeader().setReorderingAllowed(false);
        this.setTitle("Select Rule");
        this.centreWindow(this);
        this.cargarTablaReglas();
        this.filtro = fi;
        this.logminer = lgm;
        this.rule = null;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        selectRule = new javax.swing.JButton();
        go = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        reg = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tRl = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        selectRule.setText("SELECT RULE");
        selectRule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectRuleActionPerformed(evt);
            }
        });

        go.setText("NEXT");
        go.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goActionPerformed(evt);
            }
        });

        jLabel1.setText("Rule:");

        reg.setText("No Rule Selected.");

        tRl = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; //Disallow the editing of any cell
            }
        };
        tRl.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Name", "Tablespace", "Table", "Column", "Rule"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, false, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tRl);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(reg)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 636, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(selectRule, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addComponent(go, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(reg))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(go)
                    .addComponent(selectRule))
                .addGap(58, 58, 58))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void goActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goActionPerformed
        if (rule != null) {
            GUI_AddArchive add = new GUI_AddArchive(filtro, logminer, rule);
            add.setVisible(true);
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(this,"No Rule Selected.");
        }
    }//GEN-LAST:event_goActionPerformed

    private void selectRuleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectRuleActionPerformed
        int r = tRl.getSelectedRow();
        if (r == -1) {
            JOptionPane.showMessageDialog(null, "There is not rule selected.");
        } else {
            String regla = (String) tRl.getValueAt(r, 0);
            this.reg.setText(regla);
            this.setRule(regla);
        }
    }//GEN-LAST:event_selectRuleActionPerformed

    public static void centreWindow(Window frame) {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
        frame.setLocation(x, y);
    }

    public void cargarTablaReglas() {

        for (int i = 0; i < ((DefaultTableModel) tRl.getModel()).getRowCount();) {
            ((DefaultTableModel) tRl.getModel()).removeRow(i);
        }

        //agrega lo que haya en el arraylist
        for (int i = 0; i < control.getReglas().size(); i++) {
            if(control.getReglas().get(i).getServidor().equals(control.getSELECTED_SERVER()))
            {
                String ser = control.getReglas().get(i).getNombre();
                String ser2 = control.getReglas().get(i).getTablespace();
                String ser3 = control.getReglas().get(i).getTabla();
                String ser33 = control.getReglas().get(i).getColummna();
                String ser4 = control.getReglas().get(i).getColummna()+" "+control.getReglas().get(i).getCondicion()+" "+control.getReglas().get(i).getPredicado();
                ((DefaultTableModel) tRl.getModel()).addRow(new Object[]{ser,ser2,ser3,ser33,ser4});
            
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton go;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel reg;
    private javax.swing.JButton selectRule;
    private javax.swing.JTable tRl;
    // End of variables declaration//GEN-END:variables

    public String getRule() {
        return rule;
    }

    public void setRule(String rule) {
        this.rule = rule;
    }
}
